package com.novice.mq.helloworld;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class PublisherTest {
    public static void main(String[] args)throws IOException, TimeoutException {
        //1、创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //1-1、设置连接工厂的参数
        connectionFactory.setHost("47.109.145.220");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("itcast");
        connectionFactory.setPassword("123321");
        //1-2、通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //2、通过连接创建通道
        Channel channel = connection.createChannel();
        //3、创建队列
        String queueName = "simple.queue";
        channel.queueDeclare(queueName, false, false, false, null);
        //4、发布消息
        String msg = "hello rabbitMQ!fjlaksfjlasdkjflakdjflaskdjflasdkjflakdjflsjdlskjfla";
        channel.basicPublish("", queueName, null, msg.getBytes());
        System.out.println("消息发送成功！");
        // 5、关闭通道和连接
        channel.close();
        connection.close();

    }
}
